using System;
using System.Collections;
using System.Text;
using System.Web.UI.WebControls;
using PHSRAG.Insight21.Context;
using PHSRAG.Insight21.FormsManagement.Controls;
using PHSRAG.Insight21.Policy;
using PHSRAG.Utility;
using FormsManagementControls = PHSRAG.Insight21.FormsManagement.Controls;

namespace PHSRAG.Insight21.FormsManagement
{
    /// <summary>
    /// ItemEditor page loads the question item editors based on the type of the selected question
    /// </summary>
    public partial class Migrated_ItemEditor : ItemEditor
    {
        #region Constants

        private const string QuestionnaireItemTypeKey = "QuestionnaireItemTypeKey_4976D82F-7892-4edb-B1EF-DACADE6F0423";
        private const string SelectedQuestionnaireIdKey = "SelectedQuestionnaireIdKey_4E0B5E3F-881A-4888-A3B2-FCFD63AE69E5";
        private const string SelectedQuestionnaireItemIdKey = "SelectedQuestionnaireItemIdKey_68F79167-B39A-4cda-9A45-07F4655014B3";
        private const string SelectedQuestionnaireVersionIdKey = "SelectedQuestionnaireVersionIdKey_C2FD8E3D-AFF9-44aa-BE3A-8C6B6C9B0EF2";

        #endregion

        #region Instance Variables (private)

        private ICachePolicy cachePolicy;
        private ICachePolicy pageCachePolicy;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the selected questionnaire's id
        /// </summary>
        public override int SelectedQuestionnaireID
        {
            get { return int.Parse(pageCachePolicy.GetCachedObject(SelectedQuestionnaireIdKey).ToString()); }
        }

        /// <summary>
        /// Gets the selected questionnaire version's id
        /// </summary>
        public override int SelectedQuestionnaireVersionID
        {
            get { return int.Parse(pageCachePolicy.GetCachedObject(SelectedQuestionnaireVersionIdKey).ToString()); }
        }

        /// <summary>
        /// Gets the type of the question
        /// </summary>
        public override string QuestionnaireItemType
        {
            get { return pageCachePolicy.GetCachedObject(QuestionnaireItemTypeKey).ToString(); }
        }

        /// <summary>
        /// Gets the id of the selected question
        /// </summary>
        public override int SelectedQuestionnaireItemId
        {
            get { return int.Parse(pageCachePolicy.GetCachedObject(SelectedQuestionnaireItemIdKey).ToString()); }
        }

        #endregion

        #region Page Events

        /// <summary>
        /// Loads the question editor based on the type of the question and sets the breadcrumb
        /// </summary>
        /// <param name="sender">The object that raised the event.</param>
        /// <param name="e">Arguments of the event.</param>
        protected void OnPageLoad(object sender, EventArgs e)
        {
            try
            {
                if (header.ModuleName == InsightUtilities.Constants.Modules.Admin)
                {
                    adminHumansSubNavigation.Visible = true;
                    coiSubNavigation.Visible = false;
                }
                else if (header.ModuleName == InsightUtilities.Constants.Modules.COI)
                {
                    adminHumansSubNavigation.Visible = false;
                    coiSubNavigation.Visible = true;
                }

                if (!Page.IsPostBack)
                {
                    Hashtable incomingData = (Hashtable) pageCachePolicy.GetCachedObject(Keys.IncomingDataKey);
                    if (incomingData != null)
                    {
                        pageCachePolicy.Cache(SelectedQuestionnaireIdKey, incomingData[Keys.QuestionnaireIdKey].ToString());
                        pageCachePolicy.Cache(SelectedQuestionnaireVersionIdKey, incomingData[Keys.QuestionnaireVersionIdKey].ToString());
                        pageCachePolicy.Cache(QuestionnaireItemTypeKey, incomingData[Keys.QuestionnaireItemTypeKey].ToString());
                        pageCachePolicy.Cache(SelectedQuestionnaireItemIdKey, incomingData[Keys.QuestionnaireItemIdKey].ToString());
                    }
                    pageCachePolicy.UnCache(Keys.IncomingDataKey);
                }
                LoadQuestionnaireTypeControl();

                StringBuilder breadCrumbTrail = new StringBuilder(" Forms Management - ");

                switch (QuestionnaireItemType)
                {
                    case QuestionnaireItem.LabelItemType:
                        breadCrumbTrail.Append("Label");
                        break;
                    case QuestionnaireItem.TextBoxItemType:
                        breadCrumbTrail.Append("Textbox");
                        break;
                    case QuestionnaireItem.CheckBoxListItemType:
                        breadCrumbTrail.Append("Checkboxlist");
                        break;
                    case QuestionnaireItem.RadioButtonListItemType:
                        breadCrumbTrail.Append("Radiobuttonlist");
                        break;
                    case QuestionnaireItem.DropDownListItemType:
                        breadCrumbTrail.Append("Dropdownlist");
                        break;
                    case QuestionnaireItem.MultiSelectItemType:
                        breadCrumbTrail.Append("MultiInput");
                        break;
                    case QuestionnaireItem.DropDownMultiSelectItemType:
                        breadCrumbTrail.Append("DropDownMultiSelect");
                        break;
                    case QuestionnaireItem.CustomValueSelectorItemType:
                        breadCrumbTrail.Append("CustomValueSelector");
                        break;
                }
                breadCrumb.Add(breadCrumbTrail.ToString(),
                    Settings.GetKeyValue("SubNavigation.Admin.humansURL", ""),
                    Settings.GetKeyValue("BreadCrumb.SummaryPageLevel", 1));
            }
            catch
            {
                annunciator.Title = InsightUtilities.Constants.Annunciator.Title;
                annunciator.Message = "Failed to load the page.";
            }
            finally
            {
                if (coiSubNavigation.Visible)
                {
                    coiSubNavigation.SelectedLink = "Forms Management";
                    coiSubNavigation.AccessLevel = ((UserContext) cachePolicy.GetCachedObject(UserContext.CacheKey)).ModuleRoleBitmask;
                }
            }
        }

        #endregion

        #region Web Form Designer generated code

        /// <summary>
        /// Page Initialization
        /// </summary>
        /// <param name="e">Arguments of the event.</param>
        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
            pageCachePolicy = (Policy.ICachePolicy) Session[Policy.Names.PageCachePolicyKey];
            cachePolicy = (Policy.ICachePolicy) Session[Policy.Names.CachePolicyKey];
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.header.Navigate += new System.Web.UI.WebControls.CommandEventHandler(this.OnNavigate);
            this.breadCrumb.Navigate += new CommandEventHandler(OnNavigate);
            this.Load += new System.EventHandler(this.OnPageLoad);
        }

        #endregion

        #region Control Events

        /// <summary>
        /// Resets the subnavigation selected link before the header or the breadCrumb performs a redirect.
        /// </summary>
        /// <param name="sender">Control which rasied the event.</param>
        /// <param name="e">Arguments associated with the event.</param>
        private void OnNavigate(object sender, CommandEventArgs e)
        {
            try
            {
                adminSubNavigation.Reset();
                adminHumansSubNavigation.Reset();
                coiSubNavigation.Reset();
            }
            catch (Exception ex)
            {
                DisplayExceptionReport(ex);
            }
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Loads the editor based on the question type
        /// </summary>
        private void LoadQuestionnaireTypeControl()
        {
            switch (QuestionnaireItemType)
            {
                case QuestionnaireItem.LabelItemType:
                    LabelItemEditor labelItemEditor = (LabelItemEditor) Page.LoadControl("../FormsManagement/Controls/LabelItemEditor.ascx");
                    labelItemEditor.Annunciator = annunciator;
                    controlHolder.Controls.Add(labelItemEditor);
                    break;
                case QuestionnaireItem.TextBoxItemType:
                    TextBoxItemEditor textBoxItemEditor = (TextBoxItemEditor) Page.LoadControl("../FormsManagement/Controls/TextBoxItemEditor.ascx");
                    textBoxItemEditor.Annunciator = annunciator;
                    controlHolder.Controls.Add(textBoxItemEditor);
                    break;
                case QuestionnaireItem.CheckBoxListItemType:
                    CheckBoxItemEditor checkBoxItemEditor = (CheckBoxItemEditor) Page.LoadControl("../FormsManagement/Controls/CheckBoxItemEditor.ascx");
                    checkBoxItemEditor.Annunciator = annunciator;
                    controlHolder.Controls.Add(checkBoxItemEditor);
                    break;
                case QuestionnaireItem.RadioButtonListItemType:
                    RadioButtonItemEditor radioButtonItemEditor = (RadioButtonItemEditor) Page.LoadControl("../FormsManagement/Controls/RadioButtonItemEditor.ascx");
                    radioButtonItemEditor.Annunciator = annunciator;
                    controlHolder.Controls.Add(radioButtonItemEditor);
                    break;
                case QuestionnaireItem.DropDownListItemType:
                    DropDownListItemEditor dropDownListItemEditor = (DropDownListItemEditor) Page.LoadControl("../FormsManagement/Controls/DropDownListItemEditor.ascx");
                    dropDownListItemEditor.Annunciator = annunciator;
                    controlHolder.Controls.Add(dropDownListItemEditor);
                    break;
                case QuestionnaireItem.MultiSelectItemType:
                    MultiSelectItemEditor multiSelectItemEditor = (MultiSelectItemEditor) Page.LoadControl("../FormsManagement/Controls/MultiSelectItemEditor.ascx");
                    multiSelectItemEditor.Annunciator = annunciator;
                    controlHolder.Controls.Add(multiSelectItemEditor);
                    break;
                case QuestionnaireItem.DropDownMultiSelectItemType:
                    DropDownMultiSelectItemEditor dropDownMultiSelectItemEditor = (DropDownMultiSelectItemEditor) Page.LoadControl("../FormsManagement/Controls/DropDownMultiSelectItemEditor.ascx");
                    dropDownMultiSelectItemEditor.Annunciator = annunciator;
                    controlHolder.Controls.Add(dropDownMultiSelectItemEditor);
                    break;
                case QuestionnaireItem.CustomValueSelectorItemType:
                    CustomValueSelectorItemEditor customValueSelectorItemEditor = (CustomValueSelectorItemEditor) Page.LoadControl("../FormsManagement/Controls/CustomValueSelectorItemEditor.ascx");
                    customValueSelectorItemEditor.Annunciator = annunciator;
                    controlHolder.Controls.Add(customValueSelectorItemEditor);
                    break;
            }
        }

        /// <summary>
        /// Displays an error message on the screen to the user with the exception, and all
        /// inner exception messages.
        /// </summary>
        /// <param name="ex">The exception to report on</param>
        private void DisplayExceptionReport(Exception ex)
        {
            annunciator.Title = InsightUtilities.Constants.Annunciator.Title;
            annunciator.Message = ExceptionReport.GetExceptionInfo(ex, false);
        }

        #endregion
    }
}